package cn.dlx.blog.controller;

import cn.dlx.blog.service.UserService;
import cn.dlx.blog.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Map;

@RestController
public class IndexController {
    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    public Response register(@RequestBody String userName, @RequestBody String password
//            , @RequestBody(required = false)String next){
//        Map<String, String> map = userService.register(userName, password);
//        return new Response(map.get("msg"));
//    }
    public static class Register{
        private String userName;
        private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Response register(@RequestBody Register reqMap){
        Map<String, String> map = userService.register(reqMap.getUserName(), reqMap.getPassword());
        return new Response(map.get("msg"));
    }

    @RequestMapping("/login")
    public Response login(
            @RequestParam @NotBlank(message = "用户名不能为空")String username,
            @RequestParam String password){
        Map<String,String> map = userService.login(username,password);
        return new Response(map.get("msg"), map.get("ticket"));
    }
}
